package org.example.domain.strategy.service.rule.chain.factory;

import lombok.*;
import org.example.domain.strategy.model.entity.StrategyEntity;
import org.example.domain.strategy.repository.IStrategyRepository;
import org.example.domain.strategy.service.rule.chain.ILogicChain;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class DefaultChainFactory {
    private final Map<String, ILogicChain> logicChainMap;
    protected IStrategyRepository repository;
    public DefaultChainFactory(Map<String,ILogicChain> logicChainMap,IStrategyRepository repository){
        this.logicChainMap = logicChainMap;
        this.repository = repository;
    }
    public ILogicChain openLogicChain(Long strategyId){
        StrategyEntity strategy = repository.queryStrategyEntityBystrategyId(strategyId);
        String[] ruleModels = strategy.ruleModels();

        if (null ==ruleModels || 0 == ruleModels.length) return logicChainMap.get("default");
        ILogicChain logicChain = logicChainMap.get(ruleModels[0]);
        ILogicChain current = logicChain;
        for (int i = 1; i < ruleModels.length; i++) {
            ILogicChain nextChain = logicChainMap.get(ruleModels[i]);
            current = current.appendNext(nextChain);
        }
        current.appendNext(logicChainMap.get("default"));
        return logicChain;
    }
    @Getter
    @AllArgsConstructor
    public enum LogicModel{
        RULE_DEAULT("rule_deault","默认抽奖"),
        RULE_BLACK("rule_blacklist","黑名单接管"),
        RULE_WEIGHT("rule_weight","权重接管"),
        ;
        private String code;
        private String info;


    }
@Data
@AllArgsConstructor
@Builder
@NoArgsConstructor
    public static class StrategyAwardVO {
        private LogicModel logicModel;
        private Integer awardId;
    }
}
